<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="店铺">
              <j-dict-select-tag v-decorator="['storeId', validatorRules.storeId]" @change="handleStoreChange"
                                 placeholder="请选择门店" :triggerChange="true"
                                 dictCode="stm_store_info,name,store_id,status!=0"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 45px"
              label="导购">
              <a-select placeholder="请选择导购" v-decorator="['salesId', validatorRules.salesId ]"
                        :options="detailOptions"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: -130px"
              label="业绩">
              <a-input-number v-decorator="[ 'orderAmount', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: -65px"
              label="主推业绩">
              <a-input-number v-decorator="[ 'recommandAmount', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="新用户数">
              <a-input-number v-decorator="[ 'consumerCount', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="生效时间">
              <a-date-picker :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }" format='YYYY-MM-DD HH:mm:ss'
                             v-decorator="[ 'startTime', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="失效时间">
              <a-date-picker :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }" format='YYYY-MM-DD HH:mm:ss'
                             v-decorator="[ 'endTime', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'

export default {
  name: 'StmKpiSalesModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      detailOptions: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        storeId: { rules: [{ required: true, message: '请选择门店!' }] },
        salesId: { rules: [{ required: true, message: '请选择导购!' }] }
      },
      url: {
        add: '/stm/stmKpiSales/add',
        edit: '/stm/stmKpiSales/edit',
        queryList: '/stm/stmStoreInfo/queryStoreSalesRelationByMainId'
      }
    }
  },
  created() {
  },
  methods: {
    moment,
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      if (this.model && this.model.storeId) {
        this.handleStoreChange(this.model.storeId)
      }
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'storeId', 'salesId', 'consumerCount', 'recommandAmount', 'orderAmount'))
        //时间格式化
        this.form.setFieldsValue({ startTime: this.model.startTime ? moment(this.model.startTime) : null })
        this.form.setFieldsValue({ endTime: this.model.endTime ? moment(this.model.endTime) : null })
      })

    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          //时间格式化
          formData.startTime = formData.startTime ? formData.startTime.format('YYYY-MM-DD HH:mm:ss') : null
          formData.endTime = formData.endTime ? formData.endTime.format('YYYY-MM-DD HH:mm:ss') : null

          console.log(formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })


        }
      })
    },
    handleCancel() {
      this.close()
    },
    handleStoreChange(val) {
      this.form.setFieldsValue({ salesId: null })
      this.detailOptions = []
      getAction(this.url.queryList, { storeId: val }).then(res => {
        if (res.success) {
          console.log('salesData => ' + res.result)
          this.detailOptions = res.result.map(data => ({ label: data['salesName'], value: data['salesId'] }))
        } else {
          console.log(res.message)
        }
      })
    }


  }
}
</script>

<style lang="less" scoped>

</style>